package remote.layouts
{
	import flash.display.DisplayObject;
	import flash.geom.Rectangle;
	
	import remote.controls.Component;
	import remote.enums.HAlgin;
	import remote.enums.VAlgin;
	
	
	/**
	 * 平铺布局
	 * @author 银魂飞雪
	 * @createDate 2011-3-7
	 */
	public class TileLayout extends PaddingLayout
	{
		private var _halgin:String;
		private var _valgin:String;
		private var _colCount:int;
		private var _rowHeight:Number;
		private var _colWidth:Number;
		private var _hgap:Number;
		private var _vgap:Number;
		
		public function TileLayout(target:Component=null, colCount:int = 3, paddingLeft:Number=0, paddingTop:Number=0, paddingRight:Number=0, paddingBottom:Number=0, 
			hgap:Number = 5, vgap:Number = 5, rowHeight:Number = 100, colWidth:Number = 100, halgin:String = HAlgin.CENTER, valgin:String = VAlgin.MIDDLE)
		{
			super(target, paddingLeft, paddingTop, paddingRight, paddingBottom);
			this._colCount = colCount;
			_hgap = hgap;
			_vgap = vgap;
			_rowHeight = rowHeight;
			_colWidth = colWidth;
			_halgin = halgin;
			_valgin = valgin;
		}
		/**
		 * @inheritDoc 
		 * 
		 */	
		override public function layout():void
		{
			if(elements.length == 0)
				return;
			var rowC:int = rowCount, colC:int = colCount;
			for(var i:int = 0; i < elements.length; i++)
			{
				var child:DisplayObject = elements[i] as DisplayObject;
				switch(_halgin)
				{
					case HAlgin.LEFT:
						child.x = Math.round(paddingLeft +  i % colC * (_hgap + _colWidth));
						break;
					case HAlgin.CENTER:
						child.x = Math.round(paddingLeft + i % colC * (_hgap + _colWidth) + (_colWidth - child.width) / 2);
						break;
					case HAlgin.RIGHT:
						child.x = Math.round(paddingLeft + i % colC * (_hgap + _colWidth) + _colWidth - child.width);
						break;
				}
				switch(_valgin)
				{
					case VAlgin.TOP:
						child.y = Math.round(paddingTop + Math.floor(i / colC) * (_vgap + _rowHeight));
						break;
					case VAlgin.MIDDLE:
						child.y = Math.round(paddingTop + Math.floor(i / colC) * (_vgap + _rowHeight) + (_rowHeight - child.height) / 2);
						break;
					case VAlgin.BOTTOM:
						child.y =  Math.round(paddingTop + Math.floor(i / colC) * (_vgap + _rowHeight) + _rowHeight - child.height);
						break;
				}
			}
		}
		/**
		 * @inheritDoc 
		 * 
		 */	
		override public function get childrenRect():Rectangle
		{
			var w:Number = colCount * colWidth + (colCount - 1) * hgap;
			var h:Number = rowCount * rowHeight + (rowCount - 1) * vgap;
			if(colCount == 0)
				w = 0;
			if(rowCount == 0)
				h = 0;
			return new Rectangle(paddingLeft, paddingTop, w, h);
		}
		
		/**
		 * 行数 
		 * @return 行数 
		 * 
		 */		
		public function get rowCount():int
		{
			return Math.ceil(target.numChildren / colCount);
		}

		/**
		 * 列数 
		 * @return 列数 
		 * 
		 */		
		public function get colCount():int
		{
			return !isNaN(_colCount) ? _colCount : (target.width - paddingLeft - paddingRight + _hgap) / (_hgap + _colWidth);
		}

		public function set colCount(value:int):void
		{
			_colCount = value;
		}

		/**
		 * 水平方向对齐方式 
		 * @return 
		 * 
		 */		
		public function get halgin():String
		{
			return _halgin;
		}

		public function set halgin(value:String):void
		{
			_halgin = value;
		}

		/**
		 * 竖直方向对齐方式 
		 * @return 
		 * 
		 */		
		public function get valgin():String
		{
			return _valgin;
		}

		public function set valgin(value:String):void
		{
			_valgin = value;
		}

		/**
		 * 行高 
		 * @return 
		 * 
		 */		
		public function get rowHeight():Number
		{
			return _rowHeight;
		}

		public function set rowHeight(value:Number):void
		{
			_rowHeight = value;
		}

		/**
		 * 列宽 
		 * @return 
		 * 
		 */		
		public function get colWidth():Number
		{
			return _colWidth;
		}

		public function set colWidth(value:Number):void
		{
			_colWidth = value;
		}

		/**
		 * 水平方向各项间隔 
		 * @return 
		 * 
		 */		
		public function get hgap():Number
		{
			return _hgap;
		}

		public function set hgap(value:Number):void
		{
			_hgap = value;
		}
		/**
		 * 竖直方向各项间隔 
		 * @return 
		 * 
		 */
		public function get vgap():Number
		{
			return _vgap;
		}

		public function set vgap(value:Number):void
		{
			_vgap = value;
		}


	}
}